package com.e.library.utils;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.View;

/**
 *
 * @author lxy
 * @date 2015/07/08 14:39 
 * @version V1.0
 *
 */
public class EViewUtils {

    /**
     * 通用的ViewHolder
     * @param convertView
     * @param id
     * @param <V>
     * @return
     */
	public static <V extends View> V findView(View convertView,int id) {
		SparseArray<View> holder = (SparseArray<View>) convertView.getTag();
		if (holder == null) {
			holder = new SparseArray<>();
			convertView.setTag(holder);
		}
		
		View childView = holder.get(id);
		if (childView == null) {
			childView = convertView.findViewById(id);
			holder.put(id, childView);
		}
		return (V)childView;
	}

    public static void vertical(RecyclerView rec){
        setLinearLayoutManager(rec.getContext(),rec, LinearLayoutManager.VERTICAL);
    }

    public static void vertical(Fragment fragment, RecyclerView rec){
        setLinearLayoutManager(fragment.getContext(),rec, LinearLayoutManager.VERTICAL);
    }

    public static void vertical(Context context, RecyclerView rec){
        setLinearLayoutManager(context,rec, LinearLayoutManager.VERTICAL);
    }

    public static void horizontal(RecyclerView rec){
        setLinearLayoutManager(rec.getContext(),rec,LinearLayoutManager.HORIZONTAL);
    }

    public static void horizontal(Fragment fragment, RecyclerView rec){
        setLinearLayoutManager(fragment.getContext(),rec,LinearLayoutManager.HORIZONTAL);
    }

    public static void horizontal(Context context, RecyclerView rec){
        setLinearLayoutManager(context,rec,LinearLayoutManager.HORIZONTAL);
    }

    public static void setLinearLayoutManager(Context context, RecyclerView rec,int orientation){
        LinearLayoutManager manager = new LinearLayoutManager(context);
        manager.setOrientation(orientation);
        manager.setSmoothScrollbarEnabled(true);
        rec.setLayoutManager(manager);
    }
}
